<?php
namespace app\index\controller;

use think\Controller;
use \Firebase\JWT\JWT;
/**
 * 公用基础控制器
 */
class BaseController extends Controller {

	protected function _initialize() {
		parent::_initialize();
		$cut_action = request()->action();
		$jwt = isset($_SERVER['HTTP_TOKEN']) ? $_SERVER['HTTP_TOKEN'] : '';
		if ($jwt) {
			try {
				JWT::$leeway = 60;
				$decoded = JWT::decode($jwt, config('JWT_KEY'), array('HS256'));
				$arr = (array)$decoded;
				if ($arr['exp'] > time()) {
					$member = db('member')->where('password', $arr['data']->password)->find($arr['data']->id);
					if ($member) {
						session('user_id',$member['user_id']);
					}
				}
			} catch(\Exception $e) {
			}
		}
		if (!session('user_id')) {
			$this->redirect('index/index/login');
		} else {
			$this->assign('maction', request()->action());
		}
	}

	

}
